<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>xhrmanager.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_xhrmanager.js.html">xhrmanager.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Manages a pool of XhrIo&#39;s. This handles all the details of
<a name="line17"></a> * dealing with the XhrPool and provides a simple interface for sending requests
<a name="line18"></a> * and managing events.
<a name="line19"></a> *
<a name="line20"></a> * This class supports queueing &amp; prioritization of requests (XhrIoPool
<a name="line21"></a> * handles this) and retrying of requests.
<a name="line22"></a> *
<a name="line23"></a> * The events fired by the XhrManager are an aggregation of the events of
<a name="line24"></a> * each of its XhrIo objects (with some filtering, i.e., ERROR only called
<a name="line25"></a> * when there are no more retries left). For this reason, all send requests have
<a name="line26"></a> * to have an id, so that the user of this object can know which event is for
<a name="line27"></a> * which request.
<a name="line28"></a> *
<a name="line29"></a> */
<a name="line30"></a>
<a name="line31"></a>goog.provide(&#39;goog.net.XhrManager&#39;);
<a name="line32"></a>goog.provide(&#39;goog.net.XhrManager.Event&#39;);
<a name="line33"></a>goog.provide(&#39;goog.net.XhrManager.Request&#39;);
<a name="line34"></a>
<a name="line35"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line36"></a>goog.require(&#39;goog.events&#39;);
<a name="line37"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line38"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line39"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line40"></a>goog.require(&#39;goog.net.EventType&#39;);
<a name="line41"></a>goog.require(&#39;goog.net.XhrIo&#39;);
<a name="line42"></a>goog.require(&#39;goog.net.XhrIoPool&#39;);
<a name="line43"></a>goog.require(&#39;goog.structs.Map&#39;);
<a name="line44"></a>
<a name="line45"></a>// TODO(user): Add some time in between retries.
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>
<a name="line49"></a>/**
<a name="line50"></a> * A manager of an XhrIoPool.
<a name="line51"></a> * @param {number=} opt_maxRetries Max. number of retries (Default: 1).
<a name="line52"></a> * @param {goog.structs.Map=} opt_headers Map of default headers to add to every
<a name="line53"></a> *     request.
<a name="line54"></a> * @param {number=} opt_minCount Min. number of objects (Default: 1).
<a name="line55"></a> * @param {number=} opt_maxCount Max. number of objects (Default: 10).
<a name="line56"></a> * @param {number=} opt_timeoutInterval Timeout (in ms) before aborting an
<a name="line57"></a> *     attempt (Default: 0ms).
<a name="line58"></a> * @constructor
<a name="line59"></a> * @extends {goog.events.EventTarget}
<a name="line60"></a> */
<a name="line61"></a>goog.net.XhrManager = function(
<a name="line62"></a>    opt_maxRetries,
<a name="line63"></a>    opt_headers,
<a name="line64"></a>    opt_minCount,
<a name="line65"></a>    opt_maxCount,
<a name="line66"></a>    opt_timeoutInterval) {
<a name="line67"></a>  goog.base(this);
<a name="line68"></a>
<a name="line69"></a>  /**
<a name="line70"></a>   * Maximum number of retries for a given request
<a name="line71"></a>   * @type {number}
<a name="line72"></a>   * @private
<a name="line73"></a>   */
<a name="line74"></a>  this.maxRetries_ = goog.isDef(opt_maxRetries) ? opt_maxRetries : 1;
<a name="line75"></a>
<a name="line76"></a>  /**
<a name="line77"></a>   * Timeout interval for an attempt of a given request.
<a name="line78"></a>   * @type {number}
<a name="line79"></a>   * @private
<a name="line80"></a>   */
<a name="line81"></a>  this.timeoutInterval_ =
<a name="line82"></a>      goog.isDef(opt_timeoutInterval) ? Math.max(0, opt_timeoutInterval) : 0;
<a name="line83"></a>
<a name="line84"></a>  /**
<a name="line85"></a>   * The pool of XhrIo&#39;s to use.
<a name="line86"></a>   * @type {goog.net.XhrIoPool}
<a name="line87"></a>   * @private
<a name="line88"></a>   */
<a name="line89"></a>  this.xhrPool_ = new goog.net.XhrIoPool(
<a name="line90"></a>      opt_headers, opt_minCount, opt_maxCount);
<a name="line91"></a>
<a name="line92"></a>  /**
<a name="line93"></a>   * Map of ID&#39;s to requests.
<a name="line94"></a>   * @type {goog.structs.Map}
<a name="line95"></a>   * @private
<a name="line96"></a>   */
<a name="line97"></a>  this.requests_ = new goog.structs.Map();
<a name="line98"></a>
<a name="line99"></a>  /**
<a name="line100"></a>   * The event handler.
<a name="line101"></a>   * @type {goog.events.EventHandler}
<a name="line102"></a>   * @private
<a name="line103"></a>   */
<a name="line104"></a>  this.eventHandler_ = new goog.events.EventHandler(this);
<a name="line105"></a>};
<a name="line106"></a>goog.inherits(goog.net.XhrManager, goog.events.EventTarget);
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * Error to throw when a send is attempted with an ID that the manager already
<a name="line111"></a> * has registered for another request.
<a name="line112"></a> * @type {string}
<a name="line113"></a> * @private
<a name="line114"></a> */
<a name="line115"></a>goog.net.XhrManager.ERROR_ID_IN_USE_ = &#39;[goog.net.XhrManager] ID in use&#39;;
<a name="line116"></a>
<a name="line117"></a>
<a name="line118"></a>/**
<a name="line119"></a> * The goog.net.EventType&#39;s to listen/unlisten for on the XhrIo object.
<a name="line120"></a> * @type {Array.&lt;goog.net.EventType&gt;}
<a name="line121"></a> * @private
<a name="line122"></a> */
<a name="line123"></a>goog.net.XhrManager.XHR_EVENT_TYPES_ = [
<a name="line124"></a>  goog.net.EventType.READY,
<a name="line125"></a>  goog.net.EventType.COMPLETE,
<a name="line126"></a>  goog.net.EventType.SUCCESS,
<a name="line127"></a>  goog.net.EventType.ERROR,
<a name="line128"></a>  goog.net.EventType.ABORT,
<a name="line129"></a>  goog.net.EventType.TIMEOUT];
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Sets the number of milliseconds after which an incomplete request will be
<a name="line134"></a> * aborted. Zero means no timeout is set.
<a name="line135"></a> * @param {number} ms Timeout interval in milliseconds; 0 means none.
<a name="line136"></a> */
<a name="line137"></a>goog.net.XhrManager.prototype.setTimeoutInterval = function(ms) {
<a name="line138"></a>  this.timeoutInterval_ = Math.max(0, ms);
<a name="line139"></a>};
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>/**
<a name="line143"></a> * Returns the number of requests either in flight, or waiting to be sent.
<a name="line144"></a> * The count will include the current request if used within a COMPLETE event
<a name="line145"></a> * handler or callback.
<a name="line146"></a> * @return {number} The number of requests in flight or pending send.
<a name="line147"></a> */
<a name="line148"></a>goog.net.XhrManager.prototype.getOutstandingCount = function() {
<a name="line149"></a>  return this.requests_.getCount();
<a name="line150"></a>};
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/**
<a name="line154"></a> * Returns an array of request ids that are either in flight, or waiting to
<a name="line155"></a> * be sent. The id of the current request will be included if used within a
<a name="line156"></a> * COMPLETE event handler or callback.
<a name="line157"></a> * @return {!Array.&lt;string&gt;} Request ids in flight or pending send.
<a name="line158"></a> */
<a name="line159"></a>goog.net.XhrManager.prototype.getOutstandingRequestIds = function() {
<a name="line160"></a>  return this.requests_.getKeys();
<a name="line161"></a>};
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>/**
<a name="line165"></a> * Registers the given request to be sent. Throws an error if a request
<a name="line166"></a> * already exists with the given ID.
<a name="line167"></a> * NOTE: It is not sent immediately. It is queued and will be sent when an
<a name="line168"></a> * XhrIo object becomes available, taking into account the request&#39;s
<a name="line169"></a> * priority.
<a name="line170"></a> * @param {string} id The id of the request.
<a name="line171"></a> * @param {string} url Uri to make the request too.
<a name="line172"></a> * @param {string=} opt_method Send method, default: GET.
<a name="line173"></a> * @param {ArrayBuffer|Blob|Document|FormData|string=} opt_content Post data.
<a name="line174"></a> * @param {Object|goog.structs.Map=} opt_headers Map of headers to add to the
<a name="line175"></a> *     request.
<a name="line176"></a> * @param {*=} opt_priority The priority of the request. A smaller value means a
<a name="line177"></a> *     higher priority.
<a name="line178"></a> * @param {Function=} opt_callback Callback function for when request is
<a name="line179"></a> *     complete. The only param is the event object from the COMPLETE event.
<a name="line180"></a> * @param {number=} opt_maxRetries The maximum number of times the request
<a name="line181"></a> *     should be retried.
<a name="line182"></a> * @param {goog.net.XhrIo.ResponseType=} opt_responseType The response type of
<a name="line183"></a> *     this request; defaults to goog.net.XhrIo.ResponseType.DEFAULT.
<a name="line184"></a> * @return {goog.net.XhrManager.Request} The queued request object.
<a name="line185"></a> */
<a name="line186"></a>goog.net.XhrManager.prototype.send = function(
<a name="line187"></a>    id,
<a name="line188"></a>    url,
<a name="line189"></a>    opt_method,
<a name="line190"></a>    opt_content,
<a name="line191"></a>    opt_headers,
<a name="line192"></a>    opt_priority,
<a name="line193"></a>    opt_callback,
<a name="line194"></a>    opt_maxRetries,
<a name="line195"></a>    opt_responseType) {
<a name="line196"></a>  var requests = this.requests_;
<a name="line197"></a>  // Check if there is already a request with the given id.
<a name="line198"></a>  if (requests.get(id)) {
<a name="line199"></a>    throw Error(goog.net.XhrManager.ERROR_ID_IN_USE_);
<a name="line200"></a>  }
<a name="line201"></a>
<a name="line202"></a>  // Make the Request object.
<a name="line203"></a>  var request = new goog.net.XhrManager.Request(
<a name="line204"></a>      url,
<a name="line205"></a>      goog.bind(this.handleEvent_, this, id),
<a name="line206"></a>      opt_method,
<a name="line207"></a>      opt_content,
<a name="line208"></a>      opt_headers,
<a name="line209"></a>      opt_callback,
<a name="line210"></a>      goog.isDef(opt_maxRetries) ? opt_maxRetries : this.maxRetries_,
<a name="line211"></a>      opt_responseType);
<a name="line212"></a>  this.requests_.set(id, request);
<a name="line213"></a>
<a name="line214"></a>  // Setup the callback for the pool.
<a name="line215"></a>  var callback = goog.bind(this.handleAvailableXhr_, this, id);
<a name="line216"></a>  this.xhrPool_.getObject(callback, opt_priority);
<a name="line217"></a>
<a name="line218"></a>  return request;
<a name="line219"></a>};
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>/**
<a name="line223"></a> * Aborts the request associated with id.
<a name="line224"></a> * @param {string} id The id of the request to abort.
<a name="line225"></a> * @param {boolean=} opt_force If true, remove the id now so it can be reused.
<a name="line226"></a> *     No events are fired and the callback is not called when forced.
<a name="line227"></a> */
<a name="line228"></a>goog.net.XhrManager.prototype.abort = function(id, opt_force) {
<a name="line229"></a>  var request = this.requests_.get(id);
<a name="line230"></a>  if (request) {
<a name="line231"></a>    var xhrIo = request.xhrIo;
<a name="line232"></a>    request.setAborted(true);
<a name="line233"></a>    if (opt_force) {
<a name="line234"></a>      if (xhrIo) {
<a name="line235"></a>        // We remove listeners to make sure nothing gets called if a new request
<a name="line236"></a>        // with the same id is made.
<a name="line237"></a>        this.removeXhrListener_(xhrIo, request.getXhrEventCallback());
<a name="line238"></a>        goog.events.listenOnce(
<a name="line239"></a>            xhrIo,
<a name="line240"></a>            goog.net.EventType.READY,
<a name="line241"></a>            function() { this.xhrPool_.releaseObject(xhrIo); },
<a name="line242"></a>            false,
<a name="line243"></a>            this);
<a name="line244"></a>      }
<a name="line245"></a>      this.requests_.remove(id);
<a name="line246"></a>    }
<a name="line247"></a>    if (xhrIo) {
<a name="line248"></a>      xhrIo.abort();
<a name="line249"></a>    }
<a name="line250"></a>  }
<a name="line251"></a>};
<a name="line252"></a>
<a name="line253"></a>
<a name="line254"></a>/**
<a name="line255"></a> * Handles when an XhrIo object becomes available. Sets up the events, fires
<a name="line256"></a> * the READY event, and starts the process to send the request.
<a name="line257"></a> * @param {string} id The id of the request the XhrIo is for.
<a name="line258"></a> * @param {goog.net.XhrIo} xhrIo The available XhrIo object.
<a name="line259"></a> * @private
<a name="line260"></a> */
<a name="line261"></a>goog.net.XhrManager.prototype.handleAvailableXhr_ = function(id, xhrIo) {
<a name="line262"></a>  var request = this.requests_.get(id);
<a name="line263"></a>  // Make sure the request doesn&#39;t already have an XhrIo attached. This can
<a name="line264"></a>  // happen if a forced abort occurs before an XhrIo is available, and a new
<a name="line265"></a>  // request with the same id is made.
<a name="line266"></a>  if (request &amp;&amp; !request.xhrIo) {
<a name="line267"></a>    this.addXhrListener_(xhrIo, request.getXhrEventCallback());
<a name="line268"></a>
<a name="line269"></a>    // Set properties for the XhrIo.
<a name="line270"></a>    xhrIo.setTimeoutInterval(this.timeoutInterval_);
<a name="line271"></a>    xhrIo.setResponseType(request.getResponseType());
<a name="line272"></a>
<a name="line273"></a>    // Add a reference to the XhrIo object to the request.
<a name="line274"></a>    request.xhrIo = request.xhrLite = xhrIo;
<a name="line275"></a>
<a name="line276"></a>    // Notify the listeners.
<a name="line277"></a>    this.dispatchEvent(new goog.net.XhrManager.Event(
<a name="line278"></a>        goog.net.EventType.READY, this, id, xhrIo));
<a name="line279"></a>
<a name="line280"></a>    // Send the request.
<a name="line281"></a>    this.retry_(id, xhrIo);
<a name="line282"></a>
<a name="line283"></a>    // If the request was aborted before it got an XhrIo object, abort it now.
<a name="line284"></a>    if (request.getAborted()) {
<a name="line285"></a>      xhrIo.abort();
<a name="line286"></a>    }
<a name="line287"></a>  } else {
<a name="line288"></a>    // If the request has an XhrIo object already, or no request exists, just
<a name="line289"></a>    // return the XhrIo back to the pool.
<a name="line290"></a>    this.xhrPool_.releaseObject(xhrIo);
<a name="line291"></a>  }
<a name="line292"></a>};
<a name="line293"></a>
<a name="line294"></a>
<a name="line295"></a>/**
<a name="line296"></a> * Handles all events fired by the XhrIo object for a given request.
<a name="line297"></a> * @param {string} id The id of the request.
<a name="line298"></a> * @param {goog.events.Event} e The event.
<a name="line299"></a> * @return {Object} The return value from the handler, if any.
<a name="line300"></a> * @private
<a name="line301"></a> */
<a name="line302"></a>goog.net.XhrManager.prototype.handleEvent_ = function(id, e) {
<a name="line303"></a>  var xhrIo = /** @type {goog.net.XhrIo} */(e.target);
<a name="line304"></a>  switch (e.type) {
<a name="line305"></a>    case goog.net.EventType.READY:
<a name="line306"></a>      this.retry_(id, xhrIo);
<a name="line307"></a>      break;
<a name="line308"></a>
<a name="line309"></a>    case goog.net.EventType.COMPLETE:
<a name="line310"></a>      return this.handleComplete_(id, xhrIo, e);
<a name="line311"></a>
<a name="line312"></a>    case goog.net.EventType.SUCCESS:
<a name="line313"></a>      this.handleSuccess_(id, xhrIo);
<a name="line314"></a>      break;
<a name="line315"></a>
<a name="line316"></a>    // A timeout is handled like an error.
<a name="line317"></a>    case goog.net.EventType.TIMEOUT:
<a name="line318"></a>    case goog.net.EventType.ERROR:
<a name="line319"></a>      this.handleError_(id, xhrIo);
<a name="line320"></a>      break;
<a name="line321"></a>
<a name="line322"></a>    case goog.net.EventType.ABORT:
<a name="line323"></a>      this.handleAbort_(id, xhrIo);
<a name="line324"></a>      break;
<a name="line325"></a>  }
<a name="line326"></a>  return null;
<a name="line327"></a>};
<a name="line328"></a>
<a name="line329"></a>
<a name="line330"></a>/**
<a name="line331"></a> * Attempts to retry the given request. If the request has already attempted
<a name="line332"></a> * the maximum number of retries, then it removes the request and releases
<a name="line333"></a> * the XhrIo object back into the pool.
<a name="line334"></a> * @param {string} id The id of the request.
<a name="line335"></a> * @param {goog.net.XhrIo} xhrIo The XhrIo object.
<a name="line336"></a> * @private
<a name="line337"></a> */
<a name="line338"></a>goog.net.XhrManager.prototype.retry_ = function(id, xhrIo) {
<a name="line339"></a>  var request = this.requests_.get(id);
<a name="line340"></a>
<a name="line341"></a>  // If the request has not completed and it is below its max. retries.
<a name="line342"></a>  if (request &amp;&amp; !request.getCompleted() &amp;&amp; !request.hasReachedMaxRetries()) {
<a name="line343"></a>    request.increaseAttemptCount();
<a name="line344"></a>    xhrIo.send(request.getUrl(), request.getMethod(), request.getContent(),
<a name="line345"></a>        request.getHeaders());
<a name="line346"></a>  } else {
<a name="line347"></a>    if (request) {
<a name="line348"></a>      // Remove the events on the XhrIo objects.
<a name="line349"></a>      this.removeXhrListener_(xhrIo, request.getXhrEventCallback());
<a name="line350"></a>
<a name="line351"></a>      // Remove the request.
<a name="line352"></a>      this.requests_.remove(id);
<a name="line353"></a>    }
<a name="line354"></a>    // Release the XhrIo object back into the pool.
<a name="line355"></a>    this.xhrPool_.releaseObject(xhrIo);
<a name="line356"></a>  }
<a name="line357"></a>};
<a name="line358"></a>
<a name="line359"></a>
<a name="line360"></a>/**
<a name="line361"></a> * Handles the complete of a request. Dispatches the COMPLETE event and sets the
<a name="line362"></a> * the request as completed if the request has succeeded, or is done retrying.
<a name="line363"></a> * @param {string} id The id of the request.
<a name="line364"></a> * @param {goog.net.XhrIo} xhrIo The XhrIo object.
<a name="line365"></a> * @param {goog.events.Event} e The original event.
<a name="line366"></a> * @return {Object} The return value from the callback, if any.
<a name="line367"></a> * @private
<a name="line368"></a> */
<a name="line369"></a>goog.net.XhrManager.prototype.handleComplete_ = function(id, xhrIo, e) {
<a name="line370"></a>  // Only if the request is done processing should a COMPLETE event be fired.
<a name="line371"></a>  var request = this.requests_.get(id);
<a name="line372"></a>  if (xhrIo.getLastErrorCode() == goog.net.ErrorCode.ABORT ||
<a name="line373"></a>      xhrIo.isSuccess() || request.hasReachedMaxRetries()) {
<a name="line374"></a>    this.dispatchEvent(new goog.net.XhrManager.Event(
<a name="line375"></a>        goog.net.EventType.COMPLETE, this, id, xhrIo));
<a name="line376"></a>
<a name="line377"></a>    // If the request exists, we mark it as completed and call the callback
<a name="line378"></a>    if (request) {
<a name="line379"></a>      request.setCompleted(true);
<a name="line380"></a>      // Call the complete callback as if it was set as a COMPLETE event on the
<a name="line381"></a>      // XhrIo directly.
<a name="line382"></a>      if (request.getCompleteCallback()) {
<a name="line383"></a>        return request.getCompleteCallback().call(xhrIo, e);
<a name="line384"></a>      }
<a name="line385"></a>    }
<a name="line386"></a>  }
<a name="line387"></a>  return null;
<a name="line388"></a>};
<a name="line389"></a>
<a name="line390"></a>
<a name="line391"></a>/**
<a name="line392"></a> * Handles the abort of an underlying XhrIo object.
<a name="line393"></a> * @param {string} id The id of the request.
<a name="line394"></a> * @param {goog.net.XhrIo} xhrIo The XhrIo object.
<a name="line395"></a> * @private
<a name="line396"></a> */
<a name="line397"></a>goog.net.XhrManager.prototype.handleAbort_ = function(id, xhrIo) {
<a name="line398"></a>  // Fire event.
<a name="line399"></a>  // NOTE: The complete event should always be fired before the abort event, so
<a name="line400"></a>  // the bulk of the work is done in handleComplete.
<a name="line401"></a>  this.dispatchEvent(new goog.net.XhrManager.Event(
<a name="line402"></a>      goog.net.EventType.ABORT, this, id, xhrIo));
<a name="line403"></a>};
<a name="line404"></a>
<a name="line405"></a>
<a name="line406"></a>/**
<a name="line407"></a> * Handles the success of a request. Dispatches the SUCCESS event and sets the
<a name="line408"></a> * the request as completed.
<a name="line409"></a> * @param {string} id The id of the request.
<a name="line410"></a> * @param {goog.net.XhrIo} xhrIo The XhrIo object.
<a name="line411"></a> * @private
<a name="line412"></a> */
<a name="line413"></a>goog.net.XhrManager.prototype.handleSuccess_ = function(id, xhrIo) {
<a name="line414"></a>  // Fire event.
<a name="line415"></a>  // NOTE: We don&#39;t release the XhrIo object from the pool here.
<a name="line416"></a>  // It is released in the retry method, when we know it is back in the
<a name="line417"></a>  // ready state.
<a name="line418"></a>  this.dispatchEvent(new goog.net.XhrManager.Event(
<a name="line419"></a>      goog.net.EventType.SUCCESS, this, id, xhrIo));
<a name="line420"></a>};
<a name="line421"></a>
<a name="line422"></a>
<a name="line423"></a>/**
<a name="line424"></a> * Handles the error of a request. If the request has not reach its maximum
<a name="line425"></a> * number of retries, then it lets the request retry naturally (will let the
<a name="line426"></a> * request hit the READY state). Else, it dispatches the ERROR event.
<a name="line427"></a> * @param {string} id The id of the request.
<a name="line428"></a> * @param {goog.net.XhrIo} xhrIo The XhrIo object.
<a name="line429"></a> * @private
<a name="line430"></a> */
<a name="line431"></a>goog.net.XhrManager.prototype.handleError_ = function(id, xhrIo) {
<a name="line432"></a>  var request = this.requests_.get(id);
<a name="line433"></a>
<a name="line434"></a>  // If the maximum number of retries has been reached.
<a name="line435"></a>  if (request.hasReachedMaxRetries()) {
<a name="line436"></a>    // Fire event.
<a name="line437"></a>    // NOTE: We don&#39;t release the XhrIo object from the pool here.
<a name="line438"></a>    // It is released in the retry method, when we know it is back in the
<a name="line439"></a>    // ready state.
<a name="line440"></a>    this.dispatchEvent(new goog.net.XhrManager.Event(
<a name="line441"></a>        goog.net.EventType.ERROR, this, id, xhrIo));
<a name="line442"></a>  }
<a name="line443"></a>};
<a name="line444"></a>
<a name="line445"></a>
<a name="line446"></a>/**
<a name="line447"></a> * Remove listeners for XHR events on an XhrIo object.
<a name="line448"></a> * @param {goog.net.XhrIo} xhrIo The object to stop listenening to events on.
<a name="line449"></a> * @param {Function} func The callback to remove from event handling.
<a name="line450"></a> * @param {string|Array.&lt;string&gt;=} opt_types Event types to remove listeners
<a name="line451"></a> *     for. Defaults to XHR_EVENT_TYPES_.
<a name="line452"></a> * @private
<a name="line453"></a> */
<a name="line454"></a>goog.net.XhrManager.prototype.removeXhrListener_ = function(xhrIo,
<a name="line455"></a>                                                            func,
<a name="line456"></a>                                                            opt_types) {
<a name="line457"></a>  var types = opt_types || goog.net.XhrManager.XHR_EVENT_TYPES_;
<a name="line458"></a>  this.eventHandler_.unlisten(xhrIo, types, func);
<a name="line459"></a>};
<a name="line460"></a>
<a name="line461"></a>
<a name="line462"></a>/**
<a name="line463"></a> * Adds a listener for XHR events on an XhrIo object.
<a name="line464"></a> * @param {goog.net.XhrIo} xhrIo The object listen to events on.
<a name="line465"></a> * @param {Function} func The callback when the event occurs.
<a name="line466"></a> * @param {string|Array.&lt;string&gt;=} opt_types Event types to attach listeners to.
<a name="line467"></a> *     Defaults to XHR_EVENT_TYPES_.
<a name="line468"></a> * @private
<a name="line469"></a> */
<a name="line470"></a>goog.net.XhrManager.prototype.addXhrListener_ = function(xhrIo,
<a name="line471"></a>                                                         func,
<a name="line472"></a>                                                         opt_types) {
<a name="line473"></a>  var types = opt_types || goog.net.XhrManager.XHR_EVENT_TYPES_;
<a name="line474"></a>  this.eventHandler_.listen(xhrIo, types, func);
<a name="line475"></a>};
<a name="line476"></a>
<a name="line477"></a>
<a name="line478"></a>/** @override */
<a name="line479"></a>goog.net.XhrManager.prototype.disposeInternal = function() {
<a name="line480"></a>  goog.net.XhrManager.superClass_.disposeInternal.call(this);
<a name="line481"></a>
<a name="line482"></a>  this.xhrPool_.dispose();
<a name="line483"></a>  this.xhrPool_ = null;
<a name="line484"></a>
<a name="line485"></a>  this.eventHandler_.dispose();
<a name="line486"></a>  this.eventHandler_ = null;
<a name="line487"></a>
<a name="line488"></a>  // Call dispose on each request.
<a name="line489"></a>  var requests = this.requests_;
<a name="line490"></a>  goog.structs.forEach(requests, function(value, key) {
<a name="line491"></a>    value.dispose();
<a name="line492"></a>  });
<a name="line493"></a>  requests.clear();
<a name="line494"></a>  this.requests_ = null;
<a name="line495"></a>};
<a name="line496"></a>
<a name="line497"></a>
<a name="line498"></a>
<a name="line499"></a>/**
<a name="line500"></a> * An event dispatched by XhrManager.
<a name="line501"></a> *
<a name="line502"></a> * @param {goog.net.EventType} type Event Type.
<a name="line503"></a> * @param {goog.net.XhrManager} target Reference to the object that is the
<a name="line504"></a> *     target of this event.
<a name="line505"></a> * @param {string} id The id of the request this event is for.
<a name="line506"></a> * @param {goog.net.XhrIo} xhrIo The XhrIo object of the request.
<a name="line507"></a> * @constructor
<a name="line508"></a> * @extends {goog.events.Event}
<a name="line509"></a> */
<a name="line510"></a>goog.net.XhrManager.Event = function(type, target, id, xhrIo) {
<a name="line511"></a>  goog.events.Event.call(this, type, target);
<a name="line512"></a>
<a name="line513"></a>  /**
<a name="line514"></a>   * The id of the request this event is for.
<a name="line515"></a>   * @type {string}
<a name="line516"></a>   */
<a name="line517"></a>  this.id = id;
<a name="line518"></a>
<a name="line519"></a>  /**
<a name="line520"></a>   * The XhrIo object of the request.
<a name="line521"></a>   * @type {goog.net.XhrIo}
<a name="line522"></a>   */
<a name="line523"></a>  this.xhrIo = xhrIo;
<a name="line524"></a>
<a name="line525"></a>  /**
<a name="line526"></a>   * The xhrLite field aliases xhrIo for backwards compatibility.
<a name="line527"></a>   * @type {goog.net.XhrLite}
<a name="line528"></a>   */
<a name="line529"></a>  this.xhrLite = /** @type {goog.net.XhrLite} */ (xhrIo);
<a name="line530"></a>};
<a name="line531"></a>goog.inherits(goog.net.XhrManager.Event, goog.events.Event);
<a name="line532"></a>
<a name="line533"></a>
<a name="line534"></a>
<a name="line535"></a>/**
<a name="line536"></a> * An encapsulation of everything needed to make a Xhr request.
<a name="line537"></a> * NOTE: This is used internal to the XhrManager.
<a name="line538"></a> *
<a name="line539"></a> * @param {string} url Uri to make the request too.
<a name="line540"></a> * @param {Function} xhrEventCallback Callback attached to the events of the
<a name="line541"></a> *     XhrIo object of the request.
<a name="line542"></a> * @param {string=} opt_method Send method, default: GET.
<a name="line543"></a> * @param {ArrayBuffer|Blob|Document|FormData|string=} opt_content Post data.
<a name="line544"></a> * @param {Object|goog.structs.Map=} opt_headers Map of headers to add to the
<a name="line545"></a> *     request.
<a name="line546"></a> * @param {Function=} opt_callback Callback function for when request is
<a name="line547"></a> *     complete. NOTE: Only 1 callback supported across all events.
<a name="line548"></a> * @param {number=} opt_maxRetries The maximum number of times the request
<a name="line549"></a> *     should be retried (Default: 1).
<a name="line550"></a> * @param {goog.net.XhrIo.ResponseType=} opt_responseType The response type of
<a name="line551"></a> *     this request; defaults to goog.net.XhrIo.ResponseType.DEFAULT.
<a name="line552"></a> *
<a name="line553"></a> * @constructor
<a name="line554"></a> * @extends {goog.Disposable}
<a name="line555"></a> */
<a name="line556"></a>goog.net.XhrManager.Request = function(url, xhrEventCallback, opt_method,
<a name="line557"></a>    opt_content, opt_headers, opt_callback, opt_maxRetries, opt_responseType) {
<a name="line558"></a>  goog.Disposable.call(this);
<a name="line559"></a>
<a name="line560"></a>  /**
<a name="line561"></a>   * Uri to make the request too.
<a name="line562"></a>   * @type {string}
<a name="line563"></a>   * @private
<a name="line564"></a>   */
<a name="line565"></a>  this.url_ = url;
<a name="line566"></a>
<a name="line567"></a>  /**
<a name="line568"></a>   * Send method.
<a name="line569"></a>   * @type {string}
<a name="line570"></a>   * @private
<a name="line571"></a>   */
<a name="line572"></a>  this.method_ = opt_method || &#39;GET&#39;;
<a name="line573"></a>
<a name="line574"></a>  /**
<a name="line575"></a>   * Post data.
<a name="line576"></a>   * @type {ArrayBuffer|Blob|Document|FormData|string|undefined}
<a name="line577"></a>   * @private
<a name="line578"></a>   */
<a name="line579"></a>  this.content_ = opt_content;
<a name="line580"></a>
<a name="line581"></a>  /**
<a name="line582"></a>   *  Map of headers
<a name="line583"></a>   * @type {Object|goog.structs.Map|null}
<a name="line584"></a>   * @private
<a name="line585"></a>   */
<a name="line586"></a>  this.headers_ = opt_headers || null;
<a name="line587"></a>
<a name="line588"></a>  /**
<a name="line589"></a>   * The maximum number of times the request should be retried.
<a name="line590"></a>   * @type {number}
<a name="line591"></a>   * @private
<a name="line592"></a>   */
<a name="line593"></a>  this.maxRetries_ = goog.isDef(opt_maxRetries) ? opt_maxRetries : 1;
<a name="line594"></a>
<a name="line595"></a>  /**
<a name="line596"></a>   * The number of attempts  so far.
<a name="line597"></a>   * @type {number}
<a name="line598"></a>   * @private
<a name="line599"></a>   */
<a name="line600"></a>  this.attemptCount_ = 0;
<a name="line601"></a>
<a name="line602"></a>  /**
<a name="line603"></a>   * Whether the request has been completed.
<a name="line604"></a>   * @type {boolean}
<a name="line605"></a>   * @private
<a name="line606"></a>   */
<a name="line607"></a>  this.completed_ = false;
<a name="line608"></a>
<a name="line609"></a>  /**
<a name="line610"></a>   * Whether the request has been aborted.
<a name="line611"></a>   * @type {boolean}
<a name="line612"></a>   * @private
<a name="line613"></a>   */
<a name="line614"></a>  this.aborted_ = false;
<a name="line615"></a>
<a name="line616"></a>  /**
<a name="line617"></a>   * Callback attached to the events of the XhrIo object.
<a name="line618"></a>   * @type {Function|undefined}
<a name="line619"></a>   * @private
<a name="line620"></a>   */
<a name="line621"></a>  this.xhrEventCallback_ = xhrEventCallback;
<a name="line622"></a>
<a name="line623"></a>  /**
<a name="line624"></a>   * Callback function called when request is complete.
<a name="line625"></a>   * @type {Function|undefined}
<a name="line626"></a>   * @private
<a name="line627"></a>   */
<a name="line628"></a>  this.completeCallback_ = opt_callback;
<a name="line629"></a>
<a name="line630"></a>  /**
<a name="line631"></a>   * A response type to set on this.xhrIo when it&#39;s populated.
<a name="line632"></a>   * @type {!goog.net.XhrIo.ResponseType}
<a name="line633"></a>   * @private
<a name="line634"></a>   */
<a name="line635"></a>  this.responseType_ = opt_responseType || goog.net.XhrIo.ResponseType.DEFAULT;
<a name="line636"></a>
<a name="line637"></a>  /**
<a name="line638"></a>   * The XhrIo instance handling this request. Set in handleAvailableXhr.
<a name="line639"></a>   * @type {goog.net.XhrIo}
<a name="line640"></a>   */
<a name="line641"></a>  this.xhrIo = null;
<a name="line642"></a>
<a name="line643"></a>};
<a name="line644"></a>goog.inherits(goog.net.XhrManager.Request, goog.Disposable);
<a name="line645"></a>
<a name="line646"></a>
<a name="line647"></a>/**
<a name="line648"></a> * Gets the uri.
<a name="line649"></a> * @return {string} The uri to make the request to.
<a name="line650"></a> */
<a name="line651"></a>goog.net.XhrManager.Request.prototype.getUrl = function() {
<a name="line652"></a>  return this.url_;
<a name="line653"></a>};
<a name="line654"></a>
<a name="line655"></a>
<a name="line656"></a>/**
<a name="line657"></a> * Gets the send method.
<a name="line658"></a> * @return {string} The send method.
<a name="line659"></a> */
<a name="line660"></a>goog.net.XhrManager.Request.prototype.getMethod = function() {
<a name="line661"></a>  return this.method_;
<a name="line662"></a>};
<a name="line663"></a>
<a name="line664"></a>
<a name="line665"></a>/**
<a name="line666"></a> * Gets the post data.
<a name="line667"></a> * @return {ArrayBuffer|Blob|Document|FormData|string|undefined}
<a name="line668"></a> *     The post data.
<a name="line669"></a> */
<a name="line670"></a>goog.net.XhrManager.Request.prototype.getContent = function() {
<a name="line671"></a>  return this.content_;
<a name="line672"></a>};
<a name="line673"></a>
<a name="line674"></a>
<a name="line675"></a>/**
<a name="line676"></a> * Gets the map of headers.
<a name="line677"></a> * @return {Object|goog.structs.Map} The map of headers.
<a name="line678"></a> */
<a name="line679"></a>goog.net.XhrManager.Request.prototype.getHeaders = function() {
<a name="line680"></a>  return this.headers_;
<a name="line681"></a>};
<a name="line682"></a>
<a name="line683"></a>
<a name="line684"></a>/**
<a name="line685"></a> * Gets the maximum number of times the request should be retried.
<a name="line686"></a> * @return {number} The maximum number of times the request should be retried.
<a name="line687"></a> */
<a name="line688"></a>goog.net.XhrManager.Request.prototype.getMaxRetries = function() {
<a name="line689"></a>  return this.maxRetries_;
<a name="line690"></a>};
<a name="line691"></a>
<a name="line692"></a>
<a name="line693"></a>/**
<a name="line694"></a> * Gets the number of attempts so far.
<a name="line695"></a> * @return {number} The number of attempts so far.
<a name="line696"></a> */
<a name="line697"></a>goog.net.XhrManager.Request.prototype.getAttemptCount = function() {
<a name="line698"></a>  return this.attemptCount_;
<a name="line699"></a>};
<a name="line700"></a>
<a name="line701"></a>
<a name="line702"></a>/**
<a name="line703"></a> * Increases the number of attempts so far.
<a name="line704"></a> */
<a name="line705"></a>goog.net.XhrManager.Request.prototype.increaseAttemptCount = function() {
<a name="line706"></a>  this.attemptCount_++;
<a name="line707"></a>};
<a name="line708"></a>
<a name="line709"></a>
<a name="line710"></a>/**
<a name="line711"></a> * Returns whether the request has reached the maximum number of retries.
<a name="line712"></a> * @return {boolean} Whether the request has reached the maximum number of
<a name="line713"></a> *     retries.
<a name="line714"></a> */
<a name="line715"></a>goog.net.XhrManager.Request.prototype.hasReachedMaxRetries = function() {
<a name="line716"></a>  return this.attemptCount_ &gt; this.maxRetries_;
<a name="line717"></a>};
<a name="line718"></a>
<a name="line719"></a>
<a name="line720"></a>/**
<a name="line721"></a> * Sets the completed status.
<a name="line722"></a> * @param {boolean} complete The completed status.
<a name="line723"></a> */
<a name="line724"></a>goog.net.XhrManager.Request.prototype.setCompleted = function(complete) {
<a name="line725"></a>  this.completed_ = complete;
<a name="line726"></a>};
<a name="line727"></a>
<a name="line728"></a>
<a name="line729"></a>/**
<a name="line730"></a> * Gets the completed status.
<a name="line731"></a> * @return {boolean} The completed status.
<a name="line732"></a> */
<a name="line733"></a>goog.net.XhrManager.Request.prototype.getCompleted = function() {
<a name="line734"></a>  return this.completed_;
<a name="line735"></a>};
<a name="line736"></a>
<a name="line737"></a>
<a name="line738"></a>/**
<a name="line739"></a> * Sets the aborted status.
<a name="line740"></a> * @param {boolean} aborted True if the request was aborted, otherwise False.
<a name="line741"></a> */
<a name="line742"></a>goog.net.XhrManager.Request.prototype.setAborted = function(aborted) {
<a name="line743"></a>  this.aborted_ = aborted;
<a name="line744"></a>};
<a name="line745"></a>
<a name="line746"></a>
<a name="line747"></a>/**
<a name="line748"></a> * Gets the aborted status.
<a name="line749"></a> * @return {boolean} True if request was aborted, otherwise False.
<a name="line750"></a> */
<a name="line751"></a>goog.net.XhrManager.Request.prototype.getAborted = function() {
<a name="line752"></a>  return this.aborted_;
<a name="line753"></a>};
<a name="line754"></a>
<a name="line755"></a>
<a name="line756"></a>/**
<a name="line757"></a> * Gets the callback attached to the events of the XhrIo object.
<a name="line758"></a> * @return {Function|undefined} The callback attached to the events of the
<a name="line759"></a> *     XhrIo object.
<a name="line760"></a> */
<a name="line761"></a>goog.net.XhrManager.Request.prototype.getXhrEventCallback = function() {
<a name="line762"></a>  return this.xhrEventCallback_;
<a name="line763"></a>};
<a name="line764"></a>
<a name="line765"></a>
<a name="line766"></a>/**
<a name="line767"></a> * Gets the callback for when the request is complete.
<a name="line768"></a> * @return {Function|undefined} The callback for when the request is complete.
<a name="line769"></a> */
<a name="line770"></a>goog.net.XhrManager.Request.prototype.getCompleteCallback = function() {
<a name="line771"></a>  return this.completeCallback_;
<a name="line772"></a>};
<a name="line773"></a>
<a name="line774"></a>
<a name="line775"></a>/**
<a name="line776"></a> * Gets the response type that will be set on this request&#39;s XhrIo when it&#39;s
<a name="line777"></a> * available.
<a name="line778"></a> * @return {!goog.net.XhrIo.ResponseType} The response type to be set
<a name="line779"></a> *     when an XhrIo becomes available to this request.
<a name="line780"></a> */
<a name="line781"></a>goog.net.XhrManager.Request.prototype.getResponseType = function() {
<a name="line782"></a>  return this.responseType_;
<a name="line783"></a>};
<a name="line784"></a>
<a name="line785"></a>
<a name="line786"></a>/** @override */
<a name="line787"></a>goog.net.XhrManager.Request.prototype.disposeInternal = function() {
<a name="line788"></a>  goog.net.XhrManager.Request.superClass_.disposeInternal.call(this);
<a name="line789"></a>  delete this.xhrEventCallback_;
<a name="line790"></a>  delete this.completeCallback_;
<a name="line791"></a>};
</pre>


</body>
</html>
